package com.dhf.xhb.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dhf.xhb.dao.GoodsHibernateDao;
import com.dhf.xhb.dao.GoodsHibernateDaoImpl;
import com.dhf.xhb.util.PageUtil;
import com.dhf.xhb.util.StringUtil;

@Controller
public class IndexController {

	private PageUtil page;
	private String keyWord;
	private String orderBy = "goodsIncomeRatio";// 商品排列顺序 默认goodsIncome，销量goodsSales，结束时间goodsEndTime，优惠券面额
	private String goodsType;// 0"美食",1"服装",2"母婴",3"化妆品",4"居家",5"文体车品",6"数码家电",7"鞋包配饰"
	private int nowPage = 1;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		System.out.println(new Date() + " : " + request.getRemoteAddr());
		System.out.println(request.getHeader("User-Agent"));
		GoodsHibernateDao ghd = new GoodsHibernateDaoImpl();
		Map<String, String> map = new HashMap<String, String>();
		PageUtil page = new PageUtil();
		page.setNowPage(this.nowPage);
		map.put("orderBy", this.orderBy);
		if (!StringUtil.isEmpty(this.goodsType)) {
			map.put("goodsType", this.goodsType);
		}
		if (StringUtil.isEmpty(this.keyWord)) {
			page.setSearchInfo(map);
			page = ghd.queryByPage(page);
			map = page.getSearchInfo();
			if (!StringUtil.isEmpty(this.goodsType)) {
				map.put("goodsType", this.goodsType);
			}
		} else {
			this.keyWord = this.keyWord.replaceAll("[!@#^*%&',;=+?$\\x22]+", "");
			if (!StringUtil.isEmpty(this.keyWord)) {
				map.put("keyWord", this.keyWord);
				page.setSearchInfo(map);
				page = ghd.queryByKeyPage(page);
				map = page.getSearchInfo();
				map.put("keyWord", this.keyWord);
				page.setSearchInfo(map);
			} else {
				page.setSearchInfo(map);
				page = ghd.queryByPage(page);
				map = page.getSearchInfo();
				if (!StringUtil.isEmpty(this.goodsType)) {
					map.put("goodsType", this.goodsType);
				}
			}
		}
		request.setAttribute("page", page);
		response.setContentType("text/html;charset=utf-8");
	}

}
